package com.qdsgjsfk.vision.adapter;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.clj.fastble.BleManager;
import com.qdsgjsfk.vision.BR;
import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseApplication;
import com.qdsgjsfk.vision.databinding.ItemDeviceBinding;
import com.qdsgjsfk.vision.eventbus.SocketInput;
import com.qdsgjsfk.vision.model.Msg;
import com.qdsgjsfk.vision.service.BlueToothService;
import com.qdsgjsfk.vision.service.BluetoothLeService;
import com.qdsgjsfk.vision.service.SocketService;
import com.qdsgjsfk.vision.ui.DeviceListActivity;
import com.qdsgjsfk.vision.ui.InputIpActivity;
import com.qdsgjsfk.vision.ui.IpActivity;
import com.qdsgjsfk.vision.ui.MyBlueTooth;
import com.qdsgjsfk.vision.util.Constant;
import com.qdsgjsfk.vision.util.QrConfigUtil;
import com.qdsgjsfk.vision.util.ToastUtil;
import com.qdsgjsfk.vision.util.ToastUtils;
import com.qdsgjsfk.vision.util.UtilString;
import com.rest.response.DeviceResponse;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.bertsir.zbar.QrManager;
import jz.joyoung.robot.util.LogUtil;
import util.PreferencesHelper;

//import cn.bertsir.zbar.Qr.ScanResult;
//import cn.bertsir.zbar.QrConfig;
//import cn.bertsir.zbar.QrManager;
//import cn.bertsir.zbar.view.ScanLineView;


/**
 * @time 2021/3/10 14:10
 */

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.DeviceHolder> {

    public List<DeviceResponse.Device> getList() {
        return list;
    }

    public void setList(List<DeviceResponse.Device> list) {
        this.list = list;
    }

    private List<DeviceResponse.Device> list;
    private DeviceListActivity context;
    private long type;

    public DeviceListAdapter(List<DeviceResponse.Device> list, DeviceListActivity context, long type) {
        this.list = list;
        this.context = context;
        this.type = type;
    }


    @NonNull
    @Override
    public DeviceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDeviceBinding itemDeviceBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_device, parent, false);
        return new DeviceHolder(itemDeviceBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceHolder holder, int position) {
        ItemDeviceBinding itemDeviceBinding = holder.getItemDeviceBinding();
        itemDeviceBinding.setVariable(BR.deviceItem, list.get(position));
        // 1   蓝牙   2  ip
        itemDeviceBinding.setDeviceOnClick(new DeviceItemOnclick());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class DeviceHolder extends RecyclerView.ViewHolder {

        ItemDeviceBinding itemDeviceBinding;

        ItemDeviceBinding getItemDeviceBinding() {
            return itemDeviceBinding;
        }

        public void setItemDeviceBinding(ItemDeviceBinding itemDeviceBinding) {
            this.itemDeviceBinding = itemDeviceBinding;
        }

        DeviceHolder(ItemDeviceBinding itemDeviceBinding) {
            super(itemDeviceBinding.getRoot());
            this.itemDeviceBinding = itemDeviceBinding;
        }
    }

    public class DeviceItemOnclick {

        public void itemOnclick(View view, int connectType, String deviceName) {
            LogUtil.e("type设备类型--", type + "--" + connectType);
            BaseApplication.isHandheldScreenerConnected = false;
            if (type == 13) {
                //\验光仪设备名字
                PreferencesHelper.getInstance().set(context, "ygyDevice", deviceName);
            } else if (type == 12) {
                PreferencesHelper.getInstance().set(context, "slbDevice", deviceName);
            } else {
                PreferencesHelper.getInstance().set(context, "ygyDevice", deviceName);
                PreferencesHelper.getInstance().set(context, "slbDevice", deviceName);
            }

            if (connectType == 1) {
                //连接验光仪  蓝牙传输数据
                context.stopService(new Intent(context, BluetoothLeService.class));
                context.stopService(new Intent(context, BlueToothService.class));
                context.stopService(new Intent(context, SocketService.class));
//                context.startService(new Intent(context, SocketService.class));
                if (!(BleManager.getInstance().isBlueEnable())) {
                    ToastUtil.showToastCenter(context, "请先开启蓝牙");
                    return;
                }
                context.startActivity(new Intent(context, MyBlueTooth.class));
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                    context.startForegroundService( new Intent( context, SocketService.class ) );
//                } else {
//                    context.startService( new Intent( context, SocketService.class ) );
//                }
//                context.finish();
            } else if (connectType == 3) {
//                context.startActivity(new Intent(context, MyBlueTooth.class));
                context.stopService(new Intent(context, SocketService.class));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(new Intent(context, SocketService.class));
                } else {
                    context.startService(new Intent(context, SocketService.class));
                }
                ToastUtils.show(context, "请扫描视力表设备上的二维码");

                QrManager.getInstance().init(QrConfigUtil.getQrConfig()).startScan(context, result -> {
                    Log.e("onScanSuccess---", result.getContent());
                    if (!UtilString.isEmpty(result.getContent())) {
                        try {
                            JSONObject obj = new JSONObject(result.getContent());
                            String ipv4 = obj.optString("ipv4");
                            if (!UtilString.isEmpty(ipv4)) {
                                LogUtil.e("扫描二维码----", obj.optString("ipv4"));
                                PreferencesHelper.getInstance().set(context, "ipv4", obj.optString("ipv4"));
                                //通过udp去连接视力表
                                EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_QR_SUCCESS).build().toString()));
                            }
                            //finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(context, "二维码识别失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            } else if (connectType == 2) { //连接验光仪
                if ("伟伦vs100".equals(deviceName)) {
                    context.startActivity(new Intent(context, InputIpActivity.class));
                    context.finish();
                } else {
                    context.stopService(new Intent(context, SocketService.class));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        context.startForegroundService(new Intent(context, SocketService.class));
                    } else {
                        context.startService(new Intent(context, SocketService.class));
                    }
                    context.startActivity(new Intent(context, IpActivity.class));
                    context.finish();
                }
            }
        }
    }
}
